package adminController;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.BeanQuestions;
import model.Question;
import model.Utilisateur;

/**
 * Servlet implementation class ServletHomeAdmin
 */
@WebServlet("/ServletHomeAdmin")
public class ServletHomeAdmin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletHomeAdmin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession session = request.getSession(true);
		Utilisateur utilisateurCourant = (Utilisateur)session.getAttribute("Usersession");
		
		//si l'utilisateur est loger et que c'est un prof on lui donne accés à l'espace prof, sinon on le renvoi sur la page d'authentification
		if(utilisateurCourant != null && utilisateurCourant.isAdmin())
		{
			RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/admin/pageAdmin.jsp");
			dispatch.forward(request, response);
		}
		else
		{
			String strDispatch = "/Test_Projet_QCM/Authentification";
			response.sendRedirect(strDispatch);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		RequestDispatcher dispatch=null;

		
		if("Utilisateur".equals(request.getParameter("bouton")))
			dispatch = getServletContext().getRequestDispatcher("/admin/bdAjoutUtilisateur.jsp");
		else if("Question/Réponses".equals(request.getParameter("bouton")))
		{
				dispatch = getServletContext().getRequestDispatcher("/admin/bdAjoutQuestions.jsp");
		}
		else 
			if(request.getParameter("supprU")!=null)
			{
			System.out.println("Suppression Utilisateur : id="+request.getParameter("supprU"));
			
			Utilisateur user = new Utilisateur();
			try {
				user.killUser(request.getParameter("supprU"));
			} catch (ClassNotFoundException e) {
				System.out.println("Suppression Utilisateur impossible");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dispatch = getServletContext().getRequestDispatcher("/admin/bdAjoutUtilisateur.jsp");
		}
		else if(request.getParameter("supprQ")!=null){
			System.out.println("Suppression Question : id="+request.getParameter("supprQ"));
			
			Question question = new Question();
			try {
				question.killQuestion(request.getParameter("supprQ"));
			} catch (ClassNotFoundException e) {
				System.out.println("Suppression Question impossible");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dispatch = getServletContext().getRequestDispatcher("/admin/bdAjoutQuestions.jsp");
			
		
		}
		
		dispatch.forward(request, response);
		
			
	}

}
